# _*_ encoding:utf-8 _*_

"""
    自定义加密模块， 使用itsdangrous来加解密数据
"""
from itsdangerous import TimedJSONWebSignatureSerializer
from django.conf import settings


class SecretOauth:

    def __init__(self):
        self.serializer = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=3600)

    def dumps(self, content_dict):
        """加密"""
        result = self.serializer.dumps(content_dict)
        return result.decode()

    def loads(self, data):
        """解密"""
        try:
            result = self.serializer.loads(data)
        except Exception as e:
            # 解密错误
            return None

        return result

